Imports Microsoft.VisualBasic
Imports System.Data.Sql
Imports System.Data.SqlClient
Imports System
Imports System.Data
Imports GTSDALTableAdapters
Imports GTSDAL
Imports GTSSystem.BLL.IdGenerator
Imports ProfileCommon

Namespace GTSSystem.BLL
    <System.ComponentModel.DataObject()> _
    Public Class User

        <System.ComponentModel.DataObjectMethod(ComponentModel.DataObjectMethodType.Select, True)> _
        Public Shared Function getUsersByClientId(ByVal ClientID As Integer) As UserDataTable
            Dim Users As MembershipUserCollection
            Dim CurrentUser As MembershipUser = Membership.GetUser
            Dim User As MembershipUser
            Dim p As ProfileCommon
            Dim UserData As New UserDataTable
            Dim Role As String()
            Try
                Users = Membership.GetAllUsers
                If (Roles.IsUserInRole(CurrentUser.UserName, "GTSAdministrator")) Then
                    For Each User In Users
                        p = ProfileCommon.Create(User.UserName, True)
                        Role = Roles.GetRolesForUser(User.UserName)
                        UserData.AddUserRow(User.UserName, p.ClientId, p.BranchId, "mailto:" + User.Email, Role(0), p.FirstName, p.LastName, User.IsLockedOut)
                    Next
                Else
                    For Each User In Users
                        p = ProfileCommon.Create(User.UserName, True)
                        Role = Roles.GetRolesForUser(User.UserName)
                        If (p.ClientId = ClientID) Then
                            UserData.AddUserRow(User.UserName, p.ClientId, p.BranchId, "mailto:" + User.Email, Role(0), p.FirstName, p.LastName, User.IsLockedOut)
                        End If
                    Next
                End If
                getUsersByClientId = UserData
            Catch ex As Exception
                Throw
            End Try

        End Function
        <System.ComponentModel.DataObjectMethod(ComponentModel.DataObjectMethodType.Select, False)> _
        Public Shared Function getAllUsers() As UserDataTable
            Dim Users As MembershipUserCollection
            Dim CurrentUser As MembershipUser = Membership.GetUser
            Dim User As MembershipUser
            Dim p As ProfileCommon
            Dim UserData As New UserDataTable
            Dim Role As String()
            Try
                Users = Membership.GetAllUsers
                'Oly GTS Admins are allowed to see all the users
                If (Roles.IsUserInRole(CurrentUser.UserName, "GTSAdministrator")) Then
                    For Each User In Users
                        p = ProfileCommon.Create(User.UserName, True)
                        Role = Roles.GetRolesForUser(User.UserName)
                        UserData.AddUserRow(User.UserName, p.ClientId, p.BranchId, "mailto:" + User.Email, Role(0), p.FirstName, p.LastName, User.IsLockedOut)
                    Next
                End If
                getAllUsers = UserData
            Catch ex As Exception
                Throw
            End Try

        End Function
        <System.ComponentModel.DataObjectMethod(ComponentModel.DataObjectMethodType.Select, False)> _
        Public Shared Function getuserByUserName(ByVal username As String) As UserDataTable

            Dim User As MembershipUser = Membership.GetUser(username)
            Dim p As ProfileCommon
            Dim UserData As New UserDataTable
            Dim Role As String()

            Try

                p = ProfileCommon.Create(User.UserName, True)
                Role = Roles.GetRolesForUser(User.UserName)
                UserData.AddUserRow(User.UserName, p.ClientId, p.BranchId, User.Email, Role(0), p.FirstName, p.LastName, User.IsLockedOut)
                getuserByUserName = UserData

            Catch ex As Exception
                Throw
            End Try

        End Function
        <System.ComponentModel.DataObjectMethod(ComponentModel.DataObjectMethodType.Update, True)> _
         Public Shared Sub UpdateUser(ByVal UserName As String, _
                                                   ByVal ClientId As Integer, _
                                                   ByVal BranchId As Integer, _
                                                   ByVal Role As String, _
                                                   ByVal FirstName As String, _
                                                   ByVal LastName As String)
            Try
                Dim User As MembershipUser = Membership.GetUser(UserName)
                Dim p As ProfileCommon = ProfileCommon.Create(User.UserName, True)

                p.FirstName = FirstName
                p.LastName = LastName

                If (ClientId) Then
                    p.ClientId = ClientId
                End If
                If (BranchId) Then
                    p.BranchId = BranchId
                End If

                If Not (Roles.IsUserInRole(User.UserName, Role)) Then
                    Roles.AddUserToRole(UserName, Role)
                End If
                p.Save()
                Membership.UpdateUser(User)

            Catch ex As Exception
                Throw
            End Try
        End Sub

        <System.ComponentModel.DataObjectMethod(ComponentModel.DataObjectMethodType.Select, True)> _
        Public Shared Function getAllRoles() As String()
            Dim UserRoles() As String
            Try
                Dim CurrentUser As MembershipUser = Membership.GetUser
                If (Roles.IsUserInRole(CurrentUser.UserName, "GTSAdministrator")) Then
                    UserRoles = Roles.GetAllRoles
                Else
                    UserRoles = Regex.Split("Manager,User", ",")
                End If
                getAllRoles = UserRoles
            Catch ex As Exception
                Throw
            End Try
        End Function

        <System.ComponentModel.DataObjectMethod(ComponentModel.DataObjectMethodType.delete, False)> _
        Public Shared Function deleteUser(ByVal username As String) As Boolean
            Try
                Dim CurrentUser As MembershipUser = Membership.GetUser
                If ((Not (CurrentUser.UserName = username)) And (Not Roles.IsUserInRole(username, "GTSAdministrator"))) Then
                    deleteUser = Membership.DeleteUser(username)
                Else
                    Throw New ApplicationException("You cannot delete yourself!")
                End If
            Catch ex As Exception
                Throw
            End Try
        End Function

    End Class
End Namespace
